package Util;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.util.Arrays;

public class StartUtil extends JFrame {
    /*
    * 此代码是用 Swing Gui pc本地端的页面小程序
    * 业务为：根据输入的指定路径读取本地的文件件实现顺序打开MPR文件
    * NewStateMpr为1.0.1
    * */

    //声明一个面板对象
    private JPanel jp;
    //声明两个按钮对象
    private JButton btnOk, nextB, btnTLS;
    private int numClicks = 0; //计数器，计算点击次数 从下标1开始
    private Component jPanel; //弹窗

    public StartUtil() {
        super("MprStart顺序启动");
        //设置标签
        JLabel jLabel = new JLabel("输入存储的路径点击确认即可打开MPR文件");
        //创建JTextField，16表示16列，用于JTextField的宽度显示而不是限制字符个数
        JTextField textField = new JTextField(20);
        //实例化面板对象
        jp = new JPanel();
        jp.add(jLabel);
        jp.add(textField);
        //实例化两个按钮对象
        btnOk = new JButton("确认");
        nextB = new JButton("下一个");
        btnTLS = new JButton("上一个");
        //将按钮添加到面板中
        jp.add(btnOk);
        jp.add(nextB);
        jp.add(btnTLS);
        //将面板添加至窗体中
        this.add(jp);
        //设置窗口大小
        this.setSize(300, 100);
        //设置窗口左上角坐标
        this.setLocation(700, 400);
        //设置窗口关闭方式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置窗口可见
        this.setVisible(true);
        //将窗口置顶
//        this.setAlwaysOnTop(true);
        //设置窗口固定大小
        this.setResizable(false);


        btnOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取输入内容
                String str = textField.getText();

                if (str.equals("")) {
                    JOptionPane.showMessageDialog(jPanel, "请先输入存储MPR的路径！", "标题", JOptionPane.WARNING_MESSAGE);
                } else {
                    //确认按钮始终 默认打开第一个
                    File file = new File(str);
                    if (!file.exists()) {
                        JOptionPane.showMessageDialog(jPanel, "文件不存在,或者路径有误！", "标题", JOptionPane.WARNING_MESSAGE);
                    } else {
                        File[] files = file.listFiles();
                        Arrays.sort(files);
                        try {
//                            System.err.println("确认按钮"+numClicks);
                            Desktop.getDesktop().open(files[0]);
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }
            }
        });
        nextB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                String str = textField.getText();
                if (str.equals("")){
                    JOptionPane.showMessageDialog(jPanel, "路径不能为空！！！", "标题", JOptionPane.WARNING_MESSAGE);
                }else {
                    File file = new File(str);
                    if (!file.exists()){
                        JOptionPane.showMessageDialog(jPanel, "文件不存在,或者路径有误！", "标题", JOptionPane.WARNING_MESSAGE);
                    }
                    File[] files = file.listFiles();
                    try {
                        if (numClicks == files.length-1 || numClicks > files.length) {
                            System.out.println(files.length-1);
                            JOptionPane.showMessageDialog(jPanel, "您已经没有文件可以打开了", "标题", JOptionPane.WARNING_MESSAGE);
                        } else {
//                            System.err.println("下一步按钮"+numClicks);
                            //下一页按钮 下一个按钮从下标 1 开始打开 只有在打开成功后下标才进行自增
                            numClicks++;
                            Desktop.getDesktop().open(files[numClicks]);
                        }
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
            }
        });
        btnTLS.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                //获取输入内容
                String str = textField.getText();


                if (numClicks > 0){

                    if (str.equals("")){
                        JOptionPane.showMessageDialog(jPanel, "路径不能为空！！！", "标题", JOptionPane.WARNING_MESSAGE);
                    }else{
                        File file = new File(str);
                        if (!file.exists()){
                            JOptionPane.showMessageDialog(jPanel, "文件不存在,或者路径有误！", "标题", JOptionPane.WARNING_MESSAGE);
                        }
                        File[] files = file.listFiles();
                        try {
                            if (numClicks == files.length || numClicks > files.length) {
                                JOptionPane.showMessageDialog(jPanel, "现在已经是第一个文件了", "标题", JOptionPane.WARNING_MESSAGE);
                            }else {
                                //根据判断条件判断完之后 ，进减1操作
                                numClicks--;
                                Desktop.getDesktop().open(files[numClicks]);
                                System.err.println("上一步按钮当前位置"+numClicks);
                            }
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }else {
                    JOptionPane.showMessageDialog(jPanel, "现在已经是第一个文件了", "标题", JOptionPane.WARNING_MESSAGE);
                }

            }
        });
    }


    public static void main(String[] args) {
        new StartUtil();
    }


}
